package com.ruoyi.crm.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.crm.domain.IpassSchool;
import com.ruoyi.crm.mapper.IpassSchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.BaseSpaceLocationMapper;
import com.ruoyi.crm.domain.BaseSpaceLocation;
import com.ruoyi.crm.service.IBaseSpaceLocationService;

import javax.annotation.Resource;

/**
 * 空间位置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class BaseSpaceLocationServiceImpl implements IBaseSpaceLocationService {
    @Resource
    private BaseSpaceLocationMapper baseSpaceLocationMapper;

    @Resource
    private IpassSchoolMapper ipassSchoolMapper;

    /**
     * 查询空间位置
     *
     * @param id 空间位置主键
     * @return 空间位置
     */
    @Override
    public BaseSpaceLocation selectBaseSpaceLocationById(Long id) {
        BaseSpaceLocation baseSpaceLocation = baseSpaceLocationMapper.selectBaseSpaceLocationById(id);
        IpassSchool ipassSchool = ipassSchoolMapper.selectIpassSchoolBySchoolId(baseSpaceLocation.getSchoolId());
        baseSpaceLocation.setSchoolName(ipassSchool.getSchoolName());
        return baseSpaceLocation;
    }

    /**
     * 查询空间位置列表
     *
     * @param baseSpaceLocation 空间位置
     * @return 空间位置
     */
    @Override
    public List<BaseSpaceLocation> selectBaseSpaceLocationList(BaseSpaceLocation baseSpaceLocation) {
        //一级菜单
        List<BaseSpaceLocation> list = baseSpaceLocationMapper.selectBaseSpaceLocationList(baseSpaceLocation);
        for (BaseSpaceLocation b : list) {
            IpassSchool ipassSchool = ipassSchoolMapper.selectIpassSchoolBySchoolId(b.getSchoolId());
            if (null != ipassSchool) {
                b.setSchoolName(ipassSchool.getSchoolName());
            }
            //二级菜单
            List<BaseSpaceLocation> locations = baseSpaceLocationMapper.selectBaseSpaceLocationListById(b.getId());
            b.setLocations(locations);
            for (BaseSpaceLocation b1 : locations) {
                ipassSchool = ipassSchoolMapper.selectIpassSchoolBySchoolId(b1.getSchoolId());
                if (null != ipassSchool) {
                    b1.setSchoolName(ipassSchool.getSchoolName());
                }
            }
        }
        return list;
    }

    /**
     * 新增空间位置
     *
     * @param baseSpaceLocation 空间位置
     * @return 结果
     */
    @Override
    public int insertBaseSpaceLocation(BaseSpaceLocation baseSpaceLocation) {
        baseSpaceLocation.setCreateTime(DateUtils.getNowDate());
        return baseSpaceLocationMapper.insertBaseSpaceLocation(baseSpaceLocation);
    }

    /**
     * 修改空间位置
     *
     * @param baseSpaceLocation 空间位置
     * @return 结果
     */
    @Override
    public int updateBaseSpaceLocation(BaseSpaceLocation baseSpaceLocation) {
        return baseSpaceLocationMapper.updateBaseSpaceLocation(baseSpaceLocation);
    }

    /**
     * 批量删除空间位置
     *
     * @param ids 需要删除的空间位置主键
     * @return 结果
     */
    @Override
    public int deleteBaseSpaceLocationByIds(Long[] ids) {
        return baseSpaceLocationMapper.deleteBaseSpaceLocationByIds(ids);
    }

    /**
     * 删除空间位置信息
     *
     * @param id 空间位置主键
     * @return 结果
     */
    @Override
    public int deleteBaseSpaceLocationById(Long id) {
        return baseSpaceLocationMapper.deleteBaseSpaceLocationById(id);
    }
}
